package com.wn.login.utils

import android.content.Context
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.View
import android.widget.TextView
import com.blankj.utilcode.util.ColorUtils.getColor
import com.wn.login.view.PrivacyPolicyActivity

/**
 * @Author: wn
 * @Date: 2025/7/4 17:07
 * @Description: 通过 setSpan 为文本指定区域添加 点击事件 和 样式
 */

/**
 * 设置 SpannableStringBuilder 的点击事件和样式
 * @param spaBuilder SpannableStringBuilder
 * @param part String
 * @param context Context  -- 当前页面的 context
 */
fun stringSetSpan(spaBuilder: SpannableStringBuilder, part: String, context: Context) {
  spaBuilder.setSpan(
    object : ClickableSpan() {
      override fun onClick(widget: View) {
        (widget as TextView).highlightColor = getColor(com.wn.framework.R.color.transparent)
        PrivacyPolicyActivity.start(context)
      }

      override fun updateDrawState(ds: TextPaint) {
        super.updateDrawState(ds)
        ds.color = getColor(com.wn.framework.R.color.color_0165b8)
        ds.isUnderlineText = false // 取消下划线
        ds.clearShadowLayer()      // 清除阴影
      }
    },
    spaBuilder.indexOf(part),
    spaBuilder.indexOf(part) + part.length,
    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
  )
}